﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-17 12:28:29
// 负责人：liucx
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
    #region 通知消息 数据访问层
    /// <summary>
    ///通知消息 数据访问层
    /// </summary>	
    internal partial class Message : IMessage
    {

        //连接字符串
        public virtual string ConnectionStringKey
        {
            get
            {
                return ConstantUtility.CGTConnectionString;
            }
        }
        #region GetMaxID



        public int GetMaxID()
        {
            int nReturn = 0;
            string szSql = @"SELECT ISNULL(MAX(MessageID),0)+1
                               FROM [Message]";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(szSql);
            try
            {
                object o = db.ExecuteScalar(dbc);
                if (o != null)
                {
                    nReturn = Convert.ToInt32(o);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return nReturn;
        }



        #endregion

        #region Basic CRUD functions
        private MessageInfo GetByReader(IDataReader dr)
        {
            MessageInfo info = new MessageInfo();
            object obj;
            obj = dr["MessageID"];
            if (obj != null && obj != DBNull.Value)
            {
                info.MessageID = (long)obj;
            }
            obj = dr["MessageTitle"];
            if (obj != null && obj != DBNull.Value)
            {
                info.MessageTitle = obj.ToString();
            }
            obj = dr["MessageBody"];
            if (obj != null && obj != DBNull.Value)
            {
                info.MessageBody = obj.ToString();
            }
            obj = dr["CreatedDate"];
            if (obj != null && obj != DBNull.Value)
            {
                info.CreatedDate = Convert.ToDateTime(obj);
            }
            obj = dr["SendDate"];
            if (obj != null && obj != DBNull.Value)
            {
                info.SendDate = Convert.ToDateTime(obj);
            }
            obj = dr["MessageType"];
            if (obj != null && obj != DBNull.Value)
            {
                info.MessageType = Convert.ToInt32(obj);
            }
            obj = dr["MessageSource"];
            if (obj != null && obj != DBNull.Value)
            {
                info.MessageSource = Convert.ToInt32(obj);
            }
            obj = dr["OperationID"];
            if (obj != null && obj != DBNull.Value)
            {
                info.OperationID = obj.ToString();
            }
            obj = dr["MessageDesc"];
            if (obj != null && obj != DBNull.Value)
            {
                info.MessageDesc = obj.ToString();
            }
            obj = dr["IsSend"];
            if (obj != null && obj != DBNull.Value)
            {
                info.IsSend = Convert.ToInt32(obj);
            }
            return info;
        }
        public long Insert(MessageInfo info)
        {


            string strSql = @"INSERT INTO Message			
			(			
			[MessageTitle],
			[MessageBody],
			[CreatedDate],
			[SendDate],
			[MessageType],
			[MessageSource],
			[OperationID],
			[MessageDesc],
			[IsSend]
			)
			 VALUES
			(			
			@MessageTitle,
			@MessageBody,
			@CreatedDate,
			@SendDate,
			@MessageType,
			@MessageSource,
			@OperationID,
			@MessageDesc,
			@IsSend
			)
			 SET @MessageID=@@IDENTITY
			";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddOutParameter(dbc, "@MessageID", DbType.Int64, sizeof(Int64));
            db.AddInParameter(dbc, "@MessageTitle", DbType.String, info.MessageTitle);
            db.AddInParameter(dbc, "@MessageBody", DbType.String, info.MessageBody);
            db.AddInParameter(dbc, "@CreatedDate", DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@SendDate", DbType.DateTime, info.SendDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@MessageType", DbType.Int32, info.MessageType);
            db.AddInParameter(dbc, "@MessageSource", DbType.Int32, info.MessageSource);
            db.AddInParameter(dbc, "@OperationID", DbType.String, info.OperationID);
            db.AddInParameter(dbc, "@MessageDesc", DbType.String, info.MessageDesc);
            db.AddInParameter(dbc, "@IsSend", DbType.Int32, info.IsSend);


            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }
            return (long)db.GetParameterValue(dbc, "MessageID");

        }


        public void Update(MessageInfo info)
        {
            StringBuilder sbSql = new StringBuilder("UPDATE Message SET ");
            sbSql.AppendLine("[MessageTitle] = @MessageTitle,");

            sbSql.AppendLine("[MessageBody] = @MessageBody,");

            sbSql.AppendLine("[CreatedDate] = @CreatedDate,");

            sbSql.AppendLine("[SendDate] = @SendDate,");

            sbSql.AppendLine("[MessageType] = @MessageType,");

            sbSql.AppendLine("[MessageSource] = @MessageSource,");

            sbSql.AppendLine("[OperationID] = @OperationID,");

            sbSql.AppendLine("[MessageDesc] = @MessageDesc,");

            sbSql.AppendLine("[IsSend] = @IsSend");

            sbSql.Append(" where  [MessageID] = @MessageID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@MessageID", DbType.Int64, info.MessageID);
            db.AddInParameter(dbc, "@MessageTitle", DbType.String, info.MessageTitle);
            db.AddInParameter(dbc, "@MessageBody", DbType.String, info.MessageBody);
            db.AddInParameter(dbc, "@CreatedDate", DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@SendDate", DbType.DateTime, info.SendDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@MessageType", DbType.Int32, info.MessageType);
            db.AddInParameter(dbc, "@MessageSource", DbType.Int32, info.MessageSource);
            db.AddInParameter(dbc, "@OperationID", DbType.String, info.OperationID);
            db.AddInParameter(dbc, "@MessageDesc", DbType.String, info.MessageDesc);
            db.AddInParameter(dbc, "@IsSend", DbType.Int32, info.IsSend);


            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }
        }

        public void Delete(long messageID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [Message]  ");
            strSql.Append("WHERE [MessageID]=@MessageID ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@MessageID", DbType.Int64, messageID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }


        }

        public IList<MessageInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<MessageInfo> list = new List<MessageInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "Message");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    MessageInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }


        public MessageInfo GetByID(long messageID)
        {
            Model.MessageInfo info = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [Message] ");
            strSql.Append("WHERE [MessageID]=@MessageID ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@MessageID", DbType.Int64, messageID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }


        #endregion
    }
    #endregion
}